import { apiAccountList, apiDeleteAccount, apiCheckAccount } from '../../api/index'
import { getRoute } from '../../common/js/mixin'
let page = {
  /**
   * 页面的初始数据
   */
  data: {
    accountList: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.getAccountList()
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX
    })
  },
  getAccountList (load) {
    const loading = load
    apiAccountList({ loading: loading }).then(
      res => {
        this.setData({
          accountList: res.data
        })
      }
    )
  },
  editAccount (e) {
    this.selectComponent(`#cell-${e.currentTarget.dataset.item.id}`).close()
    wx.navigateTo({
      url: `../add-account/add-account?data=${JSON.stringify(e.currentTarget.dataset.item)}`
    })
  },
  deleteAccount (e) {
    wx.showModal({
      content: '确认删除该账户?',
      success: (res) => {
        if (res.confirm) {
          this.selectComponent(`#cell-${e.currentTarget.dataset.id}`).close()
          apiDeleteAccount({ id: e.currentTarget.dataset.id }).then(res => {
            this.getAccountList()
          })
        }
      }
    })
  },
  // 选择账户
  selectAccount (e) {
    if (e.currentTarget.dataset.item.id === this.id) {
      return
    }
    this.id = e.currentTarget.dataset.item.id
    apiCheckAccount({
      id: e.currentTarget.dataset.item.id,
      loading: 1
    }).then(res => {
      this.getAccountList(1)
      const route = this.getRoute()
      route.getAppli()
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        }, 2500)
      })
    })
  },
  selectNew () {
    wx.navigateTo({
      url: '../add-account/add-account'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  }
}
getRoute(page)
Page(page)
